package com.drool.mediaplayer;

import com.drool.mediaplayer.Adapters.GalleryAlbumAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class LabelViewBrowser extends Activity {

	Bundle extras;
	public static final String DATA = "DATA";
	Playlist pl;
	
	private static final String TAG = "labelViewBrowser";
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	 
	    // TODO Auto-generated method stub
	    setContentView(R.layout.laygalleryview);
	    //extras = getIntent().getExtras();
		
		Uri album = Audio.Albums.EXTERNAL_CONTENT_URI;
		final Cursor albumCursor = managedQuery(album, new String [] {
				Audio.Albums._ID, Audio.Albums.ALBUM_ART,
				Audio.Albums.ALBUM, Audio.Albums.ARTIST,
				Audio.Albums.FIRST_YEAR},
				//Audio.Albums._ID + " = " + albumId,
				null,
				null,Audio.Albums.ALBUM);
		
		startManagingCursor(albumCursor);
		//int position = extras.getInt(DATA);
		
		GalleryAlbumAdapter gAdapter = new GalleryAlbumAdapter(this, albumCursor, 0);
		Gallery g = (Gallery) findViewById(R.id.Gallery01);
		g.setAdapter(gAdapter);
		
		g.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				albumCursor.moveToPosition(position);
				Intent intent = new Intent(LabelViewBrowser.this.getApplicationContext(), 
						SongAlbumBrowser.class);
				
				intent.putExtra(SongAlbumBrowser.SELECTION, SongAlbumBrowser.SELECTION_ONE);
				intent.putExtra(SongAlbumBrowser.DATA, albumCursor.getString(0));			
				startActivity(intent);
			}
			
		});
		
		//g.setSelection(position);
		SharedPreferences settings = getSharedPreferences(MyPreferences.PREFERENCE_NAME,0);
		pl = null;
		Log.i(TAG, "settings Playlist " + settings.getLong(MyPreferences.ACTIVE_PLAYLIST, -1));
		if (settings.getLong(MyPreferences.ACTIVE_PLAYLIST, -1) == -1){
			pl = new Playlist(Playlist.PLAYLIST_TYPE_NEW, LabelViewBrowser.this, "Now Playing");
			Log.i(TAG, "Creating new playlist");
		}else{
			pl = new Playlist(settings.getLong(MyPreferences.ACTIVE_PLAYLIST, -1), LabelViewBrowser.this);
		}
		
		Log.i(TAG, "Active Playlist " + Long.toString(pl.getActivePlaylist()));
		if (pl.getActivePlaylist() == -1){
			Log.i(TAG, "No active playlist could be attached");
		}
		else{
			SharedPreferences.Editor prefEditor = settings.edit();
			prefEditor.putLong(MyPreferences.ACTIVE_PLAYLIST, pl.getActivePlaylist());					
			prefEditor.commit();
		}
		switch (settings.getInt(MyPreferences.SHUFFLE_STATE, -1)){
			case MyPreferences.SHUFFLE_STATE_VALUE_OFF:
				pl.setShuffle(Playlist.PLAYLIST_SHUFFLE_OFF);
				break;
			case MyPreferences.SHUFFLE_STATE_VALUE_ON:
				pl.setShuffle(Playlist.PLAYLIST_SHUFFLE_ON);
				break;	
			default:
				pl.setShuffle(Playlist.PLAYLIST_SHUFFLE_OFF);
				break;
		}
	
		switch (settings.getInt(MyPreferences.LOOP_STATE, -1)){
			case MyPreferences.LOOP_STATE_VALUE_OFF:
				pl.setLoop(Playlist.PLAYLIST_LOOP_OFF);
				break;
			case MyPreferences.LOOP_STATE_VALUE_ON:
				pl.setLoop(Playlist.PLAYLIST_LOOP_ON);
				break;	
			default:
				pl.setLoop(Playlist.PLAYLIST_LOOP_OFF);
				break;
		}
		
		g.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					final int position, long arg3) {
				AlertDialog alertDialog = new AlertDialog.Builder(LabelViewBrowser.this).create();
			    alertDialog.setTitle("Add album to playlist");
			    alertDialog.setMessage("Do you want to add this album to the playlist?");
			    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			    	  albumCursor.moveToPosition(position);
			    	  
			    	  Uri songUri = Audio.Media.EXTERNAL_CONTENT_URI;
			    	  
			    	  final Cursor songCursor = managedQuery(songUri, new String [] {
			    			  Audio.Media._ID
			    	  }, Audio.Media.ALBUM_ID + "= "+ albumCursor.getString(0) , null, null);
			  		  startManagingCursor(songCursor);
			  		  if (songCursor.getCount() > 0){
			  			  songCursor.moveToFirst();
			  			  while (!songCursor.isAfterLast()){
			  				try {
								pl.updatePlaylist(songCursor.getString(0));								
							} catch (InvalidPlaylistException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
			  				songCursor.moveToNext();
			  			  }
			  			Toast.makeText(LabelViewBrowser.this, "Album added to the playlist",Toast.LENGTH_SHORT).show();
			  		  }
						
			  		  songCursor.close();
			    	  return;
			      } }); 
			    alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
				      public void onClick(DialogInterface dialog, int which) {
				        return;
				      } });
				
			    alertDialog.show();
				return true;
			}
			
		});
		
	}

}
